﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RSALicCore
{
    /// <summary>
    /// 电脑硬件信息读取器
    /// </summary>
    public class HardwareInfoReader
    {
        /// <summary>
        /// 获取硬件信息的hash
        /// </summary>
        /// <returns></returns>
        public static string Read()
        {
            string cpuSerialNumber = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuSerialNumber = mo["ProcessorId"].ToString() + " - ";
                // break;
            }
            mc.Dispose();
            moc.Dispose();

            HashAlgorithm algorithm = HashAlgorithm.Create(HashAlgo);
            byte[] bytes = Encoding.UTF8.GetBytes(cpuSerialNumber);
            byte[] inArray = algorithm.ComputeHash(bytes);
            return Convert.ToBase64String(inArray);            
        }

        public static string HashAlgo = "SHA256";
    }
}
